package cg

import "C"
import (
	"fmt"
	"unsafe"
)

func IntTest(i int) C.int {
	return C.int(i + 2)
}
func StrTest(s string) *C.char {
	bt := []byte(s)
	pt := unsafe.Pointer(&bt[0])
	return (*C.char)(pt)
}

//get char* from go-string
func CharS(s string) *C.char {
	pt, _ := CharS2(s)
	return pt
}

//get char* from go-string, with c.int length
func CharS2(s string) (*C.char, C.int) {
	pt, lt, _ := CharS3(s)
	return pt, lt
}

//get char* from go-string, with c.int length and go-[]byte
func CharS3(s string) (*C.char, C.int, []byte) {
	bt := []byte(s)
	lt := len(bt)
	pt := (*C.char)(unsafe.Pointer(&bt[0]))
	return pt, C.int(lt), bt
}

//get char* from go-int-size
func CharN(size int) *C.char {
	pt, _ := CharN2(size)
	return pt
}

//get char* from go-int-size, with c.int length
func CharN2(size int) (*C.char, C.int) {
	pt, lt, _ := CharN3(size)
	return pt, lt
}

//get char* from go-int-size, with c.int length and go-[]byte
func CharN3(size int) (*C.char, C.int, []byte) {
	bt := make([]byte, size)
	lt := len(bt)
	pt := (*C.char)(unsafe.Pointer(&bt[0]))
	return pt, C.int(lt), bt
}

//get char* from go-int-size
func CharNuc(size int) *C.uchar {
	pt, _ := CharNuc2(size)
	return pt
}

//get char* from go-int-size, with c.int length
func CharNuc2(size int) (*C.uchar, C.int) {
	pt, lt, _ := CharNuc3(size)
	return pt, lt
}

//get char* from go-int-size, with c.int length and go-[]byte
func CharNuc3(size int) (*C.uchar, C.int, []byte) {
	bt := make([]byte, size)
	lt := len(bt)
	pt := (*C.uchar)(unsafe.Pointer(&bt[0]))
	return pt, C.int(lt), bt
}

func StructBuf(pt unsafe.Pointer, Len uintptr) []byte {
	return *(*[]byte)(unsafe.Pointer(&struct {
		addr uintptr
		len  int
		cap  int
	}{addr: uintptr(pt), cap: int(Len), len: int(Len)}))
}

func PrintX(buf []byte) {
	blen := len(buf)
	for i := 0; i < blen; i += 16 {
		j := i + 16
		if j > blen {
			j = blen
		}
		fmt.Printf("% X\n", buf[i:j])
	}
}
